﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NewRetail.Application.Components.WeChat.QrCode;
using NewRetail.Application.Components.WeChat.QrCode.Model;
using NewRetail.Application.Components.WeChat.QrCode.Services;
using NewRetail.Application.Core;
using System;
using System.Threading.Tasks;

namespace NewRetail.Api.Controllers.WeChatControllers
{
    [NewRetailRoute("wechat-qrcode")]
    public class WeChatQrCodeController : BaseController
    {
        #region Field
        public readonly IQrCodeService _wechatQrCodeService;
        private readonly ILogger _logger;
        #endregion

        #region Constructor
        public WeChatQrCodeController(IQrCodeService wechatQrCodeService, ILogger<WeChatQrCodeController> logger)
        {
            _wechatQrCodeService = wechatQrCodeService;
            _logger = logger;
        }
        #endregion

        #region method

        #region 创建带参数的二维码
        /// <summary>
        /// 创建带参数的二维码
        /// </summary>
        /// <param name="inputDto"></param>
        /// <remarks>ActionName为以下几个枚举值：QR_SCENE、QR_STR_SCENE、QR_LIMIT_SCENE、QR_LIMIT_STR_SCENE</remarks>
        /// <returns></returns>
        [HttpPost("create-wechatqrcode")]
        public async Task<ResponseEntity<CreateQrCodeResultModel>> CreateWeChatQrCode([FromBody] WeChatQrCodeCreateInputDto inputDto)
        {
            var ret = new ResponseEntity<CreateQrCodeResultModel>();
            var aName = inputDto.ActionName;
            var appId = inputDto.AppId;
            try
            {
                var result = (new CreateQrCodeResultModel(), "");
                if (!Enum.IsDefined(typeof(ActionNameType), aName))
                {
                    result = (null, "二维码类型不正确！");
                }
                //临时的整型
                if (aName == ActionNameType.QR_SCENE.ToString())
                {
                    var sceneId = Convert.ToUInt32(inputDto.SceneValue);
                    var expireSeconds = (uint)inputDto.ExpireSeconds;
                    var tmpQrCode = new CreateTemporaryQrCodeModel(sceneId, expireSeconds);
                    result = await _wechatQrCodeService.Create(tmpQrCode, appId, expireSeconds);
                }
                //临时的字符串
                else if (aName == ActionNameType.QR_STR_SCENE.ToString())
                {
                    var sceneStr = inputDto.SceneValue;
                    var expireSeconds = (uint)inputDto.ExpireSeconds;
                    var tmpQrCode = new CreateTemporaryQrCodeModel(sceneStr, expireSeconds);
                    result = await _wechatQrCodeService.Create(tmpQrCode, appId, expireSeconds);
                }
                //永久的整型
                else if (aName == ActionNameType.QR_LIMIT_SCENE.ToString())
                {
                    var sceneId = Convert.ToUInt32(inputDto.SceneValue);
                    var foreverQrCode = new CreateForeverQrCodeModel(sceneId);
                    result = await _wechatQrCodeService.Create(foreverQrCode, appId);
                }
                //永久的字符串
                else if (aName == ActionNameType.QR_LIMIT_STR_SCENE.ToString())
                {
                    var sceneStr = inputDto.SceneValue;
                    var foreverQrCode = new CreateForeverQrCodeModel(sceneStr);
                    result = await _wechatQrCodeService.Create(foreverQrCode, appId);
                }
                if(!string.IsNullOrEmpty(result.Item2))
                    ret.SetError("创建二维码失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch(Exception ex)
            {
                ret.SetError("创建二维码失败！" + ex.Message);
                _logger.LogError(ex.Message);
            }
            return ret;
        }
        #endregion

        #region 显示二维码
        /// <summary>
        /// 显示二维码
        /// </summary>
        /// <param name="ticket"></param>
        /// <returns></returns>
        [HttpPost("show-wechatqrcode")]
        public async Task<ResponseEntity<GetQrCodeResultModel>> ShowWeChatQrCode(string ticket)
        {
            var result = await _wechatQrCodeService.GetQrCode(ticket);
            return new ResponseEntity<GetQrCodeResultModel>().SetData(result);
        }
        #endregion

        #region 创建店铺/仓库二维码
        /// <summary>
        /// 创建店铺/仓库二维码
        /// </summary>
        /// <param name="sceneStr"></param>
        /// <param name="pixel"></param>
        /// <remarks>sceneStr为warehouse表的Id,如果不传则根据warehouse表的所有记录生成二维码存服务器</remarks>
        /// <returns></returns>
        [HttpPost("create-warehouseqrcode")]
        [Obsolete]
        public async Task<ResponseEntity<string>> CreateWeChatWarehouseQrCode(string sceneStr, int pixel = 10)
        {
            var result = new ResponseEntity<string>();
            try
            {
                var ret = await _wechatQrCodeService.CreateWarehouseQrCode(sceneStr, pixel);
                if (!ret.Item1)
                    result.SetError("创建二维码失败！");
                result.SetData(ret.Item2);
            }
            catch(Exception e)
            {
                result.SetError("创建二维码失败！" + e.Message);
                _logger.LogError(e.Message);
            }
            return result;
        }
        #endregion

        #region 创建店铺/仓库二维码并下载文件
        /// <summary>
        /// 创建店铺/仓库二维码并下载文件
        /// </summary>
        /// <param name="warehouseId"></param>
        /// <param name="pixel">图片像素大小</param>
        /// <returns></returns>
        [HttpPost("createdownload-warehouseqrcode")]
        public async Task<ResponseEntity<WarehouseQrCodeResult>> CreateAndDownloadWarehouseQrCodeFile(string warehouseId, int pixel = 5)
        {
            var result = new ResponseEntity<WarehouseQrCodeResult>();
            try
            {
                var ret = await _wechatQrCodeService.CreateWarehouseQrCode(warehouseId, pixel);
                if (!ret.Item1)
                    result.SetError("创建并下载二维码失败！" + ret.Item2);
                result.SetData(new WarehouseQrCodeResult() { WarehouseQrCodeUrls = ret.Item3 });
            }
            catch (Exception e)
            {
                result.SetError("创建并下载二维码失败！" + e.Message);
                _logger.LogError(e.Message);
            }
            return result;
        }
        #endregion

        #endregion
    }
}
